/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.businesslogic.orderprocess.impl;

import cn.ekuma.epos.businesslogic.ReceiptUtil;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.businesslogic.orderprocess.I_OrderProcessFlow;
import cn.ekuma.epos.datalogic.define.DataLogicERP;
import cn.ekuma.epos.datalogic.define.dao.CustomerDAO;
import cn.ekuma.epos.datalogic.define.dao.PaymentInfoTicketDAO;
import cn.ekuma.epos.datalogic.define.dao.ReceiptDAO;
import cn.ekuma.epos.datalogic.define.dao.UserInfoDAO;
import cn.ekuma.epos.datalogic.define.dao.erp.OrderDAO;
import cn.ekuma.epos.datalogic.define.dao.erp.OrderLineDAO;
import cn.ekuma.epos.datalogic.define.dao.erp.OrderRelationDAO;
import cn.ekuma.epos.datalogic.define.dao.erp.OrderStateDAO;
import com.openbravo.bean.BusinessPartner;
import com.openbravo.bean.Receipt;
import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.OrderLine;
import com.openbravo.bean.erp.OrderRelation;
import com.openbravo.bean.erp.OrderState;
import com.openbravo.bean.erp.OrderType;
import com.openbravo.bean.infobean.UserInfo;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.key.KeyBuilder;
import com.openbravo.data.loader.sentence.PreparedSentence;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteString;
import com.openbravo.data.loader.sentence.StaticSentence;
import com.openbravo.pos.bean.PaymentInfoTicket;

/**
 *
 * @author Administrator
 */
public class DefalutOrderProcessFlow implements I_OrderProcessFlow{

    public void saveOrder(final I_DataLogicERP dlERP1, final Order order,I_Session s) throws BasicException {
        final DataLogicERP dlERP=(DataLogicERP)dlERP1;
        final ReceiptDAO receiptDAO=(ReceiptDAO) dlERP.getDao(Receipt.class);
        final OrderDAO orderDAO=(OrderDAO) dlERP.getDao(Order.class);
        final OrderLineDAO orderLineDAO=(OrderLineDAO) dlERP.getDao(OrderLine.class);
        final OrderStateDAO orderStateDAO=(OrderStateDAO) dlERP.getDao(OrderState.class);
        final OrderRelationDAO orderRelationDAO=(OrderRelationDAO)dlERP.getDao(OrderRelation.class);
        final OrderType oType = order.getDefineType();
                if (oType.getFundType() != OrderType.FUNDTYPE_NoChange) {
                    // new receipt
                    receiptDAO.insert(ReceiptUtil.buildReceiptByOrder(order));
                }
                // new order
                orderDAO.insert(order);
                for (OrderLine l : order.getLines()) {
                    orderLineDAO.insert(l);
                }

               if (order.getRefOrders() != null) {
                    for (final OrderRelation orderRelation : order.getRefOrders()) {
                    	orderRelationDAO.insert(orderRelation);
                    }
                }

                
                if (order.getStates() != null) {
                    for (final OrderState orderState : order.getStates()) {
                        orderStateDAO.insert(orderState);
                    }
                }
                
                new FundOrderProcess().saveOrder(dlERP1, order,s);
                new OrderReferenceOrderProcess().saveOrder(dlERP1, order,s);
                new PriceOrderProcess().saveOrder(dlERP1, order,s);
                new StoreOrderProcess().saveOrder(dlERP1, order,s);
                new CustomerProductOrderProcess().saveOrder(dlERP1, order,s);
    }

    public void deleteOrder(final I_DataLogicERP dlERP1, final Order order,I_Session s) throws BasicException {

    	new FundOrderProcess().deleteOrder(dlERP1, order,s);
         new StoreOrderProcess().deleteOrder(dlERP1, order,s);
         new OrderReferenceOrderProcess().deleteOrder(dlERP1, order,s);
                
                // and delete the receipt
                new StaticSentence(s, "DELETE FROM TAXLINES WHERE RECEIPT = ?", SerializerWriteString.INSTANCE).exec(order.getId());
                new StaticSentence(s, "DELETE FROM ORDERLINES WHERE ORDERRID = ?", SerializerWriteString.INSTANCE).exec(order.getId());
                new StaticSentence(s, "DELETE FROM ORDERSTATES WHERE ORDERID = ?", SerializerWriteString.INSTANCE).exec(order.getId());
                new StaticSentence(s, "DELETE FROM ORDERS WHERE ID = ?", SerializerWriteString.INSTANCE).exec(order.getId());
                new StaticSentence(s, "DELETE FROM RECEIPTS WHERE ID = ?", SerializerWriteString.INSTANCE).exec(order.getId());
               
    }
    
    public Order loadOrder(final I_DataLogicERP dlERP1,final String orderid,I_Session s) throws BasicException {
        final DataLogicERP dlERP=(DataLogicERP)dlERP1;
    	final OrderDAO orderDAO=(OrderDAO) dlERP.getDao(Order.class);
    	CustomerDAO customerDAO=(CustomerDAO)dlERP.getDao(BusinessPartner.class);
    	PaymentInfoTicketDAO paymentInfoTicketDAO=(PaymentInfoTicketDAO)dlERP.getDao(PaymentInfoTicket.class); 
    	OrderLineDAO orderLineDAO=(OrderLineDAO)dlERP.getDao(OrderLine.class);
    	UserInfoDAO userInfoDAO=(UserInfoDAO)dlERP.getDao(UserInfo.class);
    	Order order = orderDAO.findOrderByOrderId(orderid);
        if (order != null) {
        	order.setUser(userInfoDAO.loadPeopleByID(order.getUser().getId()));
            String customerid = order.getCustomerId();
            order.setCustomer(customerid == null
                    ? null
                    : customerDAO.find(KeyBuilder.getKey(customerid)));
            order.setDefineType(dlERP.findOrderTypeById(order.getDefineType().getId()));
            order.setLines(new PreparedSentence(s, "SELECT L.ID,L.ORDERRID, L.LINE, L.PRODUCT, L.ATTRIBUTESETINSTANCE_ID, L.UNITS, L.PRICE, T.ID, T.NAME, T.CATEGORY, T.CUSTCATEGORY, T.PARENTID, T.RATE, T.RATECASCADE, T.RATEORDER, L.ATTRIBUTES, L.DEFAULTPRICE, L.PRICERATE, L.ISPREFSELL,L.CONSULUNIT,L.CONSULTPRICE,L.DIFFPRICE,L.DIFFPRICECOST  "
                    + "FROM ORDERLINES L, TAXES T WHERE L.TAXID = T.ID AND L.ORDERRID = ? ORDER BY L.LINE", SerializerWriteString.INSTANCE, orderLineDAO).list(order.getId()));
            if (order.getDefineType().getFundType() != OrderType.FUNDTYPE_NoChange) {
            	order.setPayments(paymentInfoTicketDAO.listByReceipt(order.getId()));
             }
            order.setRefOrders(dlERP.listOrderRelationInfoByOrder(order.getId()));
            order.setReference_User(order.getReference_User() == null
                    ? null
                    : userInfoDAO.loadPeopleByID(order.getReference_User().getId()));
            
			order.setStates(dlERP.listOrderStateByOrder(order.getId()));
        }
        return order;
    }

    public String getName() {
        return PROCESSTYPE_Default;
    }

}
